imcontext: Eat key events during preedit
authorMatthias Clasen <mclasen@redhat.com>
Thu, 15 Apr 2021 20:59:02 +0000 (16:59 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Thu, 15 Apr 2021 20:59:02 +0000 (16:59 -0400)
Avoid passing through random key press or release
events while we are showing preedit. That prevents
'accidents' like typing Ctrl-. bringing up the
Emoji chooser during preedit, or hitting Ctrl-a
after the Compose key moving the 'dot' around in
vim in terminals.

gtk/gtkimcontextsimple.c

index 71044294e55f40193063198a10e3797b337a6c32..a3a2c54c8f4306888ca3e194b6e5f48b602e5162 100644 (file)
@@ -761,13 +761,21 @@ gtk_im_context_simple_filter_keypress (GtkIMContext *context,
            }
        }
 
+      if (priv->in_hex_sequence || priv->in_compose_sequence)
+        return TRUE; /* Don't leak random key events during preedit */
+
       return FALSE;
     }
 
   /* Ignore modifier key presses */
   for (i = 0; i < G_N_ELEMENTS (gtk_compose_ignore); i++)
     if (keyval == gtk_compose_ignore[i])
-      return FALSE;
+      {
+        if (priv->in_hex_sequence || priv->in_compose_sequence)
+          return TRUE; /* Don't leak random key events during preedit */
+
+        return FALSE;
+      }
 
   hex_mod_mask = GDK_CONTROL_MASK|GDK_SHIFT_MASK;
 
@@ -802,16 +810,23 @@ gtk_im_context_simple_filter_keypress (GtkIMContext *context,
 
       no_text_input_mask = GDK_ALT_MASK|GDK_CONTROL_MASK;
 
-      if (state & no_text_input_mask ||
-         (priv->in_hex_sequence && priv->modifiers_dropped &&
-          (keyval == GDK_KEY_Return ||
-           keyval == GDK_KEY_ISO_Enter ||
-           keyval == GDK_KEY_KP_Enter)))
+      if (priv->in_hex_sequence && priv->modifiers_dropped &&
+         (keyval == GDK_KEY_Return ||
+          keyval == GDK_KEY_ISO_Enter ||
+          keyval == GDK_KEY_KP_Enter))
        {
          return FALSE;
        }
+
+      if (state & no_text_input_mask)
+        {
+          if (priv->in_hex_sequence || priv->in_compose_sequence)
+            return TRUE; /* Don't leak random key events during preedit */
+
+          return FALSE;
+        }
     }
-  
+
   /* Handle backspace */
   if (priv->in_hex_sequence && have_hex_mods && is_backspace)
     {